import { _decorator, Color, Component, Graphics, Node, Vec2, Vec3 } from 'cc';
import { BlackHoleManager } from './BlackHoleManager';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('BlackHoleItem')
export class BlackHoleItem extends Component {

    @property(Graphics)
    Graphics1: Graphics = null;

    @property(Graphics)
    Graphics2: Graphics = null;

    @property
    Radius: number = 10;

    @property
    SubRadius: number = 2;

    private _color: Color = new Color();
    private _velocity: Vec2 = new Vec2();
    private _dis: number = 0;
    private _suctionConstant: number;
    private _timer: number = 0;
    private _isPause: boolean = false;

    init(parent: Node, pos: Vec3, dis: number, suctionConstant: number) {
        this.node.parent = parent;
        this.node.setWorldPosition(pos);

        this._dis = dis;
        this._suctionConstant = suctionConstant;

        this._color = this.getRandomBrightColor();
        this.drawCircle();
        this._timer = 1;
    }

    protected update(dt: number): void {
        if (!BlackHoleManager.Instance.IsStart) return;
        if (this._isPause) return;
        this._timer -= dt;
        const delta = new Vec2(this._velocity.x * dt, this._velocity.y * dt);

        let cowPos2D = new Vec2(this.node.getWorldPosition().x, this.node.getWorldPosition().y).add(delta);

        // === 检查边界 ===
        const center2D = new Vec2(BlackHoleManager.Instance.CententPos.x, BlackHoleManager.Instance.CententPos.y);
        const dist = cowPos2D.clone().subtract(center2D).length();

        if (dist + this.Radius >= BlackHoleManager.Instance.CircleRadius) {
            // 计算法线
            const normal = cowPos2D.clone().subtract(center2D).normalize();

            // 反射速度
            const reflectDir = this.reflect(this._velocity.clone(), normal.clone());

            // // 加随机角度
            // const randomAngle = randomRange(-5, 5);
            // const rotated = this.rotateVector(reflectDir, randomAngle);

            // 更新速度和加速
            this._velocity = reflectDir.multiplyScalar(0.5);
            // this._velocity = normal.clone().negative();

            if (this._timer < 0) BlackHoleManager.Instance.addArc(this._color, normal.clone());
            // 推回边界
            cowPos2D = center2D.add(normal.multiplyScalar(BlackHoleManager.Instance.CircleRadius - this.Radius));
            this.node.setWorldPosition(new Vec3(cowPos2D.x, cowPos2D.y, this.node.getWorldPosition().z));
        } else {
            this.node.setWorldPosition(this.node.getWorldPosition().add3f(this._velocity.x * dt, this._velocity.y * dt, 0));
        }

        const dis: number = BlackHoleManager.Instance.getDis(this.node);
        if (dis < this._dis) {
            BlackHoleManager.Instance.removeItem(this.node);
            return;
        }

        // 计算引力的方向
        let direction = BlackHoleManager.Instance.getDir(this.node);
        // 计算引力向量
        let force = direction.multiplyScalar(this._suctionConstant + dis / 30);
        // 引力施加到当前物体的速度
        this._velocity = this._velocity.add2f(force.x, force.y);
        // this._addVelocity = this._addVelocity.add2f(force.x, math.clamp(force.y, 0, 10));
    }

    drawCircle() {
        this.Graphics1.clear();

        this.Graphics1.circle(0, 0, this.Radius);
        this.Graphics1.fillColor = Color.WHITE;
        this.Graphics1.fill();

        this.Graphics2.clear();

        this.Graphics2.circle(0, 0, this.Radius - this.SubRadius);
        this.Graphics2.fillColor = this._color;
        this.Graphics2.fill();
    }


    //获取比较亮的颜色
    getRandomBrightColor(): Color {
        let r = Math.floor(128 + Math.random() * 128);
        let g = Math.floor(128 + Math.random() * 128);
        let b = Math.floor(128 + Math.random() * 128);
        return new Color(r, g, b, 255);
    }


    /** 向量反射 */
    private reflect(v: Vec2, n: Vec2): Vec2 {
        // v - 2*(v·n)*n
        const dot = v.dot(n);
        return v.subtract(n.multiplyScalar(2 * dot));
    }

    /** 向量旋转 */
    private rotateVector(v: Vec2, angleDeg: number): Vec2 {
        const rad = angleDeg * Math.PI / 180;
        const cos = Math.cos(rad);
        const sin = Math.sin(rad);
        return new Vec2(
            v.x * cos - v.y * sin,
            v.x * sin + v.y * cos
        );
    }

    private pause() {
        this._isPause = !this._isPause;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }
}


